package com.smedi.ismedi.inmail.core.application.query.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smedi.ismedi.authority.starter.util.AuthorityUtil;
import com.smedi.ismedi.inmail.core.application.command.AnnouncementListQuery;
import com.smedi.ismedi.inmail.core.application.command.AnnouncementOneQuery;
import com.smedi.ismedi.inmail.core.application.command.AnnouncementPageQuery;
import com.smedi.ismedi.inmail.core.application.command.DownloadFileToZipCommand;
import com.smedi.ismedi.inmail.core.application.convert.AppConverter;
import com.smedi.ismedi.inmail.core.application.query.AnnouncementQueryService;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.announcement.Announcement;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.announcement.AttachmentKeyAndTime;
import com.smedi.ismedi.inmail.core.domain.dto.AnnouncementQuery;
import com.smedi.ismedi.inmail.core.domain.dto.DownloadFileToZip;
import com.smedi.ismedi.inmail.core.domain.repository.AnnouncementRepository;
import com.smedi.ismedi.personnel.api.dto.request.OrganizationAssignmentListQueryRequest;
import com.smedi.ismedi.personnel.api.dto.response.resp.OrganizationAssignmentResp;
import com.smedi.ismedi.personnel.api.feign.RemotePersonCoreFeignApi;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.smedi.ismedi.inmail.core.infrastructure.constant.AnnouncementStatusEnum.C_PUBLISHED;
import static com.smedi.ismedi.inmail.core.infrastructure.constant.ConstantField.ACCESS_HR_ORGANIZATION;

/**
 * 查询服务实现类
 *
 * @author jiangqinxuan
 * @date 2021/07/30
 */
@Service
@AllArgsConstructor
public class AnnouncementQueryServiceImpl implements AnnouncementQueryService {
    private final RemotePersonCoreFeignApi remotePersonCoreFeignApi;

    private final AnnouncementRepository announcementRepository;

    private final AuthorityUtil authorityUtil;

    private final AppConverter appConverter;

    @Override
    public Announcement getAnnouncementOne(AnnouncementOneQuery announcementOneQuery) {
        AnnouncementQuery announcementQuery = appConverter.announcementOneQueryToAnnouncementQuery(announcementOneQuery);
        return announcementRepository.getAnnouncementOne(announcementQuery);
    }

    @Override
    public List<Announcement> getAnnouncementList(AnnouncementListQuery announcementListQuery) {
        AnnouncementQuery announcementQuery = appConverter.announcementListQueryToAnnouncementQuery(announcementListQuery);
        return announcementRepository.getAnnouncementList(announcementQuery);
    }

    @Override
    public List<Announcement> getAnnouncementListInManagement(AnnouncementListQuery announcementListQuery) {
        AnnouncementQuery announcementQuery = appConverter.announcementListQueryToAnnouncementQuery(announcementListQuery);
        Set<Long> publisherIds = new HashSet<>();
        // 仅提供发布人为当前用户权限范围内的组织
        List<Long> organizationIdsWithAccess = authorityUtil.getDataScope().getOrDefault(ACCESS_HR_ORGANIZATION, new ArrayList<>());
        List<OrganizationAssignmentResp> organizationAssignmentResps = remotePersonCoreFeignApi.getOrganizationAssignmentList(OrganizationAssignmentListQueryRequest.builder().hrOrganizationIds(new ArrayList<>(organizationIdsWithAccess)).build()).getData();
        if (CollectionUtils.isNotEmpty(organizationAssignmentResps)) {
            publisherIds.addAll(organizationAssignmentResps.stream().map(OrganizationAssignmentResp::getPersonId).collect(Collectors.toList()));
        }

        announcementQuery.setPublisherIds(new ArrayList<>(publisherIds));
        return announcementRepository.getAnnouncementList(announcementQuery);
    }

    @Override
    public Page<Announcement> getAnnouncementPageInManagement(AnnouncementPageQuery announcementPageQuery) {
        AnnouncementQuery announcementQuery = appConverter.announcementPageQueryToAnnouncementQuery(announcementPageQuery);
        Set<Long> publisherIds = new HashSet<>();
        // 仅提供发布人为当前用户权限范围内的组织
        List<Long> organizationIdsWithAccess = authorityUtil.getDataScope().getOrDefault(ACCESS_HR_ORGANIZATION, new ArrayList<>());
        List<OrganizationAssignmentResp> organizationAssignmentResps = remotePersonCoreFeignApi.getOrganizationAssignmentList(OrganizationAssignmentListQueryRequest.builder().hrOrganizationIds(new ArrayList<>(organizationIdsWithAccess)).build()).getData();
        if (CollectionUtils.isNotEmpty(organizationAssignmentResps)) {
            publisherIds.addAll(organizationAssignmentResps.stream().map(OrganizationAssignmentResp::getPersonId).collect(Collectors.toList()));
        }

        announcementQuery.setPublisherIds(new ArrayList<>(publisherIds));
        return announcementRepository.getAnnouncementPage(announcementQuery);
    }

    @Override
    public Page<Announcement> getAnnouncementPageInPerson(AnnouncementPageQuery announcementPageQuery) {
        AnnouncementQuery announcementQuery = appConverter.announcementPageQueryToAnnouncementQuery(announcementPageQuery);
        announcementQuery.setReceiverId(authorityUtil.getPersonId());
        announcementQuery.setStatus(C_PUBLISHED.toString());
        return announcementRepository.getAnnouncementPage(announcementQuery);
    }

    @Override
    public Page<Announcement> getAnnouncementPage(AnnouncementPageQuery announcementPageQuery) {
        AnnouncementQuery announcementQuery = appConverter.announcementPageQueryToAnnouncementQuery(announcementPageQuery);
        announcementQuery.setAboutMeId(authorityUtil.getPersonId());
        return announcementRepository.getAnnouncementPage(announcementQuery);
    }

    @Override
    public List<AttachmentKeyAndTime> getAttachmentKeyList(DownloadFileToZipCommand downloadFileToZipCommand) {
        DownloadFileToZip downloadFileToZip = appConverter.downloadFileToZipCommandToDownloadFileToZip(downloadFileToZipCommand);
        return announcementRepository.getAttachmentKeyList(downloadFileToZip);
    }
}
